<script setup>
import { ref, nextTick, onMounted } from "vue";
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入折线图图表，图表后缀都为 Chart
import { PieChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 标签自动布局、全局过渡动画等特性
import { UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";
import waterContent from "@/assets/waterContent/content.json";

// 注册必须的组件
echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  PieChart,
  UniversalTransition,
  CanvasRenderer,
]);

// 接收数据
const props = defineProps({
  typeOf: {
    type: String,
    default: "",
  },
  content: {
    type: Object,
    default: function () {
      return {};
    },
  },
});

let contentData = [];
const initData = () => {
  for (const key in props.content) {
    let a = {
      value: props.content[key].toFixed(2),
      name:
        waterContent[props.typeOf][key]["name"] +
        "(" +
        props.content[key].toFixed(2) +
        ")",
    };
    contentData.push(a);
  }
};
initData();

// 初始化图表
const optionForm = {
  legend: {},
  toolbox: {
    show: true,
    feature: {
      mark: { show: true },
    },
  },
  tooltip: {
    trigger: "item",
  },
  series: [
    {
      name: "Nightingale Chart",
      type: "pie",
      radius: ["0%", "65%"],
      center: ["50%", "60%"],
      roseType: "area",
      itemStyle: {
        borderRadius: 8,
      },
      data: [],
    },
  ],
};
const option = ref({ ...optionForm });

let containerRef = ref(null);
const initChart = () => {
  // 接下来的使用就跟之前一样，初始化图表，设置配置项
  const myChart = echarts.init(containerRef.value);
  // 加数据
  option.value.series[0].data = contentData;
  myChart.setOption(option.value);
};

onMounted(() => {
  nextTick(() => {
    initChart();
  });
});
</script>

<template>
  <div ref="containerRef" id="container"></div>
</template>

<style lang="scss" scoped>
#container {
  height: 100%;
  width: 100%;
}
</style>